package com.yunwaikeji.gy_tool_library.widget.recycler;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.yunwaikeji.gy_tool_library.adapter.recycler.GYRecyclerAdapter;
import com.yunwaikeji.gy_tool_library.adapter.recycler.GYViewHolder;
import com.yunwaikeji.gy_tool_library.info.GYRecyclerAdapterInfo;
import com.yunwaikeji.gy_tool_library.utils.GYLogUtils;
import com.yunwaikeji.gy_tool_library.widget.GYLoadingLayout;

import java.util.List;

public class GYAdapterRefresh<Model> implements GYRecyclerAdapterInfo {
    private GYRecyclerAdapter<Model, ? extends GYViewHolder> recyclerAdapter;
    private List<Model> dataList;
    private GYLoadingLayout loadingLayout;
    private SmartRefreshLayout smartRefreshLayout;
    private boolean refresh = true;//是否刷新，true：刷新，false：加载
    private int pageNumber = 0;
    private int pageSize = 10;
    private RefreshPageType refreshPageType = RefreshPageType.ADD_PAGE_SIZE;

    public void refresh() {
        if (recyclerAdapter == null) {
            GYLogUtils.printError(this, "recyclerAdapter不能为空");
            return;
        }
        if (loadingLayout != null) {
            loadingLayout.showContent();
        }
        if (refresh) {
            if (dataList.size() < pageSize) {
                if (smartRefreshLayout != null) {
                    smartRefreshLayout.finishRefreshWithNoMoreData();
                }
                if (loadingLayout != null && dataList.isEmpty()) {
                    loadingLayout.showError("数据为空\n\n点击刷新");
                }
            } else {
                if (smartRefreshLayout != null) {
                    smartRefreshLayout.finishRefresh();
                }
                if (refreshPageType == RefreshPageType.ADD_PAGE_NUMBER) {
                    pageNumber++;
                } else if (refreshPageType == RefreshPageType.ADD_PAGE_SIZE) {
                    pageNumber = dataList.size();
                }
            }
            recyclerAdapter.setData(dataList);
        } else {
            if (dataList.size() < pageSize) {
                if (smartRefreshLayout != null) {
                    smartRefreshLayout.finishLoadMoreWithNoMoreData();
                }
            } else {
                if (smartRefreshLayout != null) {
                    smartRefreshLayout.finishLoadMore();
                }
                if (refreshPageType == RefreshPageType.ADD_PAGE_NUMBER) {
                    pageNumber++;
                } else if (refreshPageType == RefreshPageType.ADD_PAGE_SIZE) {
                    pageNumber += dataList.size();
                }
            }
            recyclerAdapter.addData(dataList);
        }
    }

    public GYRecyclerAdapter<Model, ?> getRecyclerAdapter() {
        return recyclerAdapter;
    }

    public GYAdapterRefresh<Model> setRecyclerAdapter(GYRecyclerAdapter<Model, ?> recyclerAdapter) {
        this.recyclerAdapter = recyclerAdapter;
        return this;
    }

    public List<Model> getDataList() {
        return dataList;
    }

    public GYAdapterRefresh<Model> setDataList(List<Model> dataList) {
        this.dataList = dataList;
        return this;
    }

    public GYLoadingLayout getLoadingLayout() {
        return loadingLayout;
    }

    public GYAdapterRefresh<Model> setLoadingLayout(GYLoadingLayout loadingLayout) {
        this.loadingLayout = loadingLayout;
        return this;
    }

    public SmartRefreshLayout getSmartRefreshLayout() {
        return smartRefreshLayout;
    }

    public GYAdapterRefresh<Model> setSmartRefreshLayout(SmartRefreshLayout smartRefreshLayout) {
        this.smartRefreshLayout = smartRefreshLayout;
        return this;
    }

    public boolean isRefresh() {
        return refresh;
    }

    public GYAdapterRefresh<Model> setRefresh(boolean refresh) {
        this.refresh = refresh;
        if (refresh) {
            if (refreshPageType == RefreshPageType.ADD_PAGE_NUMBER) {
                pageNumber = 1;
            } else if (refreshPageType == RefreshPageType.ADD_PAGE_SIZE) {
                pageNumber = 0;
            }
        }
        return this;
    }

    public int getPageSize() {
        return pageSize;
    }

    public GYAdapterRefresh<Model> setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public GYAdapterRefresh<Model> setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
        return this;
    }

    public RefreshPageType getRefreshPageType() {
        return refreshPageType;
    }

    public GYAdapterRefresh<Model> setRefreshPageType(RefreshPageType refreshPageType) {
        this.refreshPageType = refreshPageType;
        return this;
    }

    public GYAdapterRefresh<Model> bind(GYRecyclerView recyclerView, GYRecyclerAdapter<Model, ? extends GYViewHolder> recyclerAdapter) {
        recyclerView.setAdapter(recyclerAdapter);
        this.recyclerAdapter = recyclerAdapter;
        return this;
    }
}
